package com.ly.algorithm.offerpointer;

import java.util.Arrays;

/**
 * @Classname Offer21
 * @Description TODO
 * @Date 2021/1/27 14:51
 * @Author 冷心影翼
 */
public class Offer21 {

    public static void main(String[] args) {
        Solution21 solution21 = new Solution21();
        int[] exchange = solution21.exchange(new int[]{1, 2, 3, 4, 5});
        int[] exchange2 = solution21.exchangeByFastLow(new int[]{1, 2, 3, 4, 5});
        System.out.println(Arrays.toString(exchange));
        System.out.println(Arrays.toString(exchange2));
    }
}


class Solution21 {


    //双指针，类似快排
    public int[] exchange(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        while (left<right) {
            while (left<right && nums[left]%2 != 0) {
                left++;
            }
            while (left<right && nums[right]%2 == 0) {
                right--;
            }
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
        return nums;
    }

    //快慢指针
    public int[] exchangeByFastLow(int[] nums) {
        int fast = 0;
        int slow = 0;
        while (fast<nums.length) {
            if((nums[fast] & 1) == 1) {
                int temp = nums[fast];
                nums[fast] = nums[slow];
                nums[slow] = temp;
                slow++;
            }
            fast++;
        }
        return nums;
    }
}